'use strict';

stareal
    .controller("CollectController", function ($scope, $lazyLoader, $api, $state,$alert,$stateParams) {
       // $scope.goods = new $lazyLoader("app/favor/list",{},true);
        // $scope.comments = new $lazyLoader("app/comment/retrieve", {},true)
        $scope.changeNav=$stateParams.kind;
        $scope.banji=true;
        $scope.bianjiText='编辑';
        $scope.currentIndex=$stateParams.kind-1;
        $scope.seleviewId=[];
        $scope.seleviewType=[];
        $scope.seleId=[];
        //他人观演记列表
        $scope.getMoreclassifyli=function(){
            if($scope.changeNav==1){   //观演记
                $scope.goodcomments = new $lazyLoader("app/view/collectionlist",{flag:1},true)
            }else if($scope.changeNav==2){ //票务
                $scope.goods = new $lazyLoader("app/favor/list",{},true);
            }else if($scope.changeNav==3){ //攻略
                $scope.gongcomments=new $lazyLoader("app/celebritys/collectionlist",{},true)
            }else if($scope.changeNav==4){ //美图
                $scope.BeautifulImgLists=new $lazyLoader("app/activity/collectionlist",{type:8},true)
            }else if($scope.changeNav==5){ //活动 app/activity/collectionlist  老  app/activity/collectionlist
                $scope.dongcomments=new $lazyLoader("app/activity/collectionactivitylist",{},true)
            } else if($scope.changeNav==6){ //资讯
                $scope.newscomments=new $lazyLoader("app/activity/collectionlist",{type:6},true)
            }else if($scope.changeNav==7){ //视频
                $scope.videoscomments=new $lazyLoader("app/activity/collectionlistvideo",{},true)
            }

        }
        $('#demo05').navbarscroll({
            defaultSelect:$scope.currentIndex,

        });
        $scope.getMoreclassifyli();
        var refresh = function(){
            $state.go('my.collect',{kind:$scope.changeNav},true);
        };
        $scope.navSwitch=function(index,ind){
            $scope.changeNav=index;
            $scope.currentIndex=ind;
            refresh();
        }
        //编辑收藏
        $scope.changeBianJ=function(){
            if($scope.banji){
                $scope.banji=false;
                $scope.bianjiText='完成';
            }else{
                $scope.seleviewId=[];
                $scope.seleviewType=[];
                $scope.seleId=[];
                $scope.banji=true;
                $scope.bianjiText='编辑';
            }
        }

        //取消收藏   观演记 app/view/collection  viewid=18050416
        $scope.selGo=function(viewId,index,type){
            if( $scope.changeNav==1) {
                if ($scope.banji) {
                    $state.go('main.viewdetail', {id: viewId}, true);
                } else {
                    if ($("#collect_" + index).find('.icon_select').hasClass('selected')) {
                        $("#collect_" + index).find('.icon_select').removeClass("selected");
                    } else {
                        $("#collect_" + index).find('.icon_select').addClass("selected");
                    }

                }
            }else if( $scope.changeNav==2){
                if ($scope.banji) {
                    $state.go('main.detail', {good_id: viewId}, true);
                } else {
                    if ($("#collects_" + index).find('.icon_select').hasClass('selected')) {
                        $("#collects_" + index).find('.icon_select').removeClass("selected");
                    } else {
                        $("#collects_" + index).find('.icon_select').addClass("selected");
                    }

                }

            }else if($scope.changeNav==3){  //攻略 main.raidersdetail
                if ($scope.banji) {
                    $state.go('main.raidersdetail', {id: viewId}, true);
                } else {
                    if ($("#gongcollect_" + index).find('.icon_select').hasClass('selected')) {
                        $("#gongcollect_" + index).find('.icon_select').removeClass("selected");
                    } else {
                        $("#gongcollect_" + index).find('.icon_select').addClass("selected");
                    }

                }
            }else if($scope.changeNav==4){  //美图 main.raidersdetail
                if ($scope.banji) { //ui-sref="main.beautifulImgDetail({id:good.id})"
                    $state.go('main.beautifulImgDetail', {id: viewId}, true);
                } else {
                    if ($("#Beautifulcollect_" + index).find('.icon_select').hasClass('selected')) {
                        $("#Beautifulcollect_" + index).find('.icon_select').removeClass("selected");
                    } else {
                        $("#Beautifulcollect_" + index).find('.icon_select').addClass("selected");
                    }

                }
            }else if($scope.changeNav==5){  //活动 main.raiseactivedetail
                    if ($scope.banji) {
                        if(type==3){
                            $state.go('main.raiseactivedetail', {id: viewId}, true);
                        }else if(type==7){
                            $state.go('main.activedetail', {id: viewId}, true);
                        }

                    } else {
                        if ($("#dongcollect_" + index).find('.icon_select').hasClass('selected')) {
                            $("#dongcollect_" + index).find('.icon_select').removeClass("selected");
                        } else {
                            $("#dongcollect_" + index).find('.icon_select').addClass("selected");
                        }

                    }
            }else if($scope.changeNav==6){  //资讯 main.raiseactivedetail
                if ($scope.banji) {
                    $state.go('main.newdetail', {newId: viewId}, true);
                } else {
                    if ($("#newscollect_" + index).find('.icon_select').hasClass('selected')) {
                        $("#newscollect_" + index).find('.icon_select').removeClass("selected");
                    } else {
                        $("#newscollect_" + index).find('.icon_select').addClass("selected");
                    }

                }
            } else if($scope.changeNav==7){  //视频 main.raiseactivedetail
                if ($scope.banji) {
                    $state.go('main.videodetail', {id: viewId}, true);
                } else {
                    if ($("#videoscollect_" + index).find('.icon_select').hasClass('selected')) {
                        $("#videoscollect_" + index).find('.icon_select').removeClass("selected");
                    } else {
                        $("#videoscollect_" + index).find('.icon_select').addClass("selected");
                    }

                }
            }
        }
        //取消收藏   观演记 app/view/collection  viewid=18050416
        $scope.delviewCollect=function(){
            if( $scope.changeNav==1) {
                var selectArr = $(".viewcollect .selected");
                if (selectArr.length == 0) {
                    $alert.show("请选择取消收藏的观演记!");
                    return false;
                } else {
                    for (var i = 0; i < selectArr.length; i++) {
                        $scope.seleviewId.push($(selectArr[i]).attr("data-id"));
                    }
                    var viewID = $scope.seleviewId.join(',');
                    $api.post("app/view/collection", {viewid: viewID}, true)
                        .then(function (ret) {
                            location.reload();
                        }, function (err) {
                            $alert.show(err)
                        })
                }
             }else if( $scope.changeNav==2){
                var selectArr =$(".piao_flag .selected");
                if (selectArr.length == 0) {
                    $alert.show("请选择取消收藏的演出!");
                    return false;
                } else {
                   // console.log(selectArr);
                    for (var i = 0; i < selectArr.length; i++) {
                        $scope.seleId.push($(selectArr[i]).attr("data-id"));
                    }
                    var viewID = $scope.seleId.join(',');
                    $api.post("app/favor/create", {good_id: viewID}, true)
                        .then(function (ret) {
                            location.reload();
                        }, function (err) {
                            $alert.show(err)
                        })
                }
             }else  if( $scope.changeNav==3) {
                var selectArr = $(".gongcollect .selected");
                if (selectArr.length == 0) {
                    $alert.show("请选择取消收藏的攻略!");
                    return false;
                } else {
                    for (var i = 0; i < selectArr.length; i++) {
                        $scope.seleviewId.push($(selectArr[i]).attr("data-id"));
                    }
                    var viewID = $scope.seleviewId.join(',');
                    $api.post("app/view/collection", {viewid: viewID,type:4}, true)
                        .then(function (ret) {
                            location.reload();
                        }, function (err) {
                            $alert.show(err)
                        })
                }
            }else  if( $scope.changeNav==4) {
                var selectArr = $(".Beautifulcollect .selected");
                if (selectArr.length == 0) {
                    $alert.show("请选择取消收藏的场馆!");
                    return false;
                } else {
                    for (var i = 0; i < selectArr.length; i++) {
                        $scope.seleviewId.push($(selectArr[i]).attr("data-id"));//data-type
                    }
                    var viewID = $scope.seleviewId.join(',');
                    $api.post("app/view/collection", {viewid: viewID,type:8}, true)
                        .then(function (ret) {
                            location.reload();
                        }, function (err) {
                            $alert.show(err)
                        })
                }
            }else  if( $scope.changeNav==5) {  //活动
                var selectArr = $(".dongcollect .selected");
                if (selectArr.length == 0) {
                    $alert.show("请选择取消收藏的活动!");
                    return false;
                } else {
                    for (var i = 0; i < selectArr.length; i++) {
                        $scope.seleviewId.push($(selectArr[i]).attr("data-id"));
                        $scope.seleviewType.push($(selectArr[i]).attr("data-type"));
                    }
                    var viewID = $scope.seleviewId.join(',');
                    var type=$scope.seleviewType.join(',');  //老接口
                    $api.post("app/view/collectionactivity", {viewid: viewID,type:type}, true)
                        .then(function (ret) {
                            location.reload();
                        }, function (err) {
                            $alert.show(err)
                        })
                }
            }else  if( $scope.changeNav==6) {
                var selectArr = $(".newscollect .selected");
                if (selectArr.length == 0) {
                    $alert.show("请选择取消收藏的资讯!");
                    return false;
                } else {
                    for (var i = 0; i < selectArr.length; i++) {
                        $scope.seleviewId.push($(selectArr[i]).attr("data-id"));
                    }
                    var viewID = $scope.seleviewId.join(',');
                    $api.post("app/view/collection", {viewid: viewID,type:6}, true)
                        .then(function (ret) {
                            location.reload();
                        }, function (err) {
                            $alert.show(err)
                        })
                }
            }else  if( $scope.changeNav==7) {
                var selectArr = $(".videoscollect .selected");
                if (selectArr.length == 0) {
                    $alert.show("请选择取消收藏的视频!");
                    return false;
                } else {
                    for (var i = 0; i < selectArr.length; i++) {
                        $scope.seleviewId.push($(selectArr[i]).attr("data-id"));
                    }
                    var viewID = $scope.seleviewId.join(',');
                    $api.post("app/view/collection", {viewid: viewID,type:5}, true)
                        .then(function (ret) {
                            location.reload();
                        }, function (err) {
                            $alert.show(err)
                        })
                }
            }

            }
    });